package com.xyre.client.business.base;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xyre.client.R;
import com.xyre.client.framework.util.DebugLog;


/**
 * 视图基类
 *
 * @author 792793182@qq.com 2015-09-18.
 */
public final class BaseView extends RelativeLayout {

    private static final String TAG = BaseView.class.getSimpleName();

    /**
     * 标题栏文本
     */
    private TextView titleTv;

    /**
     * 返回按钮
     */
    private ImageButton backBtn;

    /**
     * 标题栏右边的按钮
     */
    private ImageView rightBtn;

    public BaseView(Context context) {
        this(context, false);
    }

    public BaseView(Context context, boolean needHead) {
        super(context);

        View.inflate(context, R.layout.base_activity, this);

        if (needHead) {
            inflateHead();
        }
    }

    public BaseView addContentView(View view, ViewGroup.LayoutParams params) {
        ViewGroup body = (ViewGroup) findViewById(R.id.base_activity_body);
        body.addView(view, params);
        return this;
    }

    public BaseView addContentView(View view) {
        ViewGroup body = (ViewGroup) findViewById(R.id.base_activity_body);
        body.addView(view, new ViewGroup.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
        return this;
    }

    /**
     * 注意：这几个重载的方法不同互相调用，因为子类可能会重写，会出现不肯想象的结果
     *
     * @param layoutId 布局文件的ID
     */
    public BaseView addContentView(int layoutId) {
        ViewGroup body = (ViewGroup) findViewById(R.id.base_activity_body);
        body.addView(View.inflate(getContext(), layoutId, null), new ViewGroup.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
        return this;
    }

    /**
     * 隐藏掉头部
     */
    public final BaseView hideHeadView() {
        findViewById(R.id.base_activity_head).setVisibility(View.GONE);
        return this;
    }

    /**
     * 显示头部
     */
    public final BaseView showHeadView() {
        findViewById(R.id.base_activity_head).setVisibility(View.VISIBLE);

        if (titleTv == null || backBtn == null || rightBtn == null) {
            inflateHead();
        }
        return this;
    }

    public final BaseView setTitle(CharSequence title) {
        if (titleTv != null) {
            titleTv.setText(title);
        }
        return this;
    }

    public final BaseView setTitle(int titleId) {
        String title = "";
        try {
            title = getContext().getString(titleId);
            setTitle(title);
        } catch (Exception e) {
            DebugLog.e(TAG, "setTitle()", e);
        }
        return this;
    }

    public final BaseView setTitleTextSize(float size) {
        if (titleTv != null) {
            titleTv.setTextSize(size);
        }
        return this;
    }

    /**
     * 设置返回按钮的点击事件的监听器
     * @param onClickListener
     */
    public void setBackBtnClickListener(OnClickListener onClickListener) {
        if (backBtn != null) {
            backBtn.setOnClickListener(onClickListener);
        }
    }

    /**
     * 设置右边按钮的点击事件的监听器
     * @param onClickListener
     */
    public void setRightBtnClickListener(OnClickListener onClickListener) {
        if (rightBtn != null) {
            rightBtn.setOnClickListener(onClickListener);
        }
    }

    private void inflateHead() {
        titleTv = (TextView) findViewById(R.id.base_activity_head_title_tv);
        backBtn = (ImageButton) findViewById(R.id.base_activity_head_back_btn);
        rightBtn = (ImageView) findViewById(R.id.base_activity_head_right_btn);
    }

    public ImageView getRightBtn() {
        return rightBtn;
    }

    public ImageButton getBackBtn() {
        return backBtn;
    }
}
